﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using STX.Admin.Models.BizSupplier;
using STX.Service;
using STX.Domain;
using STX.Web.Framework;
namespace STX.Admin.Controllers
{
    public class BizSupplierController : BasePages1Controller<BizSupplier,IndexModel,AddModel,EditModel,QueryModel>
    {
        public BizSupplierService BizSupplierService { get; set; }
        public BizAreaService BizAreaService { get; set; }
        public BizQuotePriceTemplateService BizQuotePriceTemplateService { get; set; }

        #region 初始化
        protected override void OnInit(out Func<BaseService<BizSupplier>> thiService,
            out Func<IndexModel> getIndexModelFunc, 
            out Func<AddModel> getAddModelFunc, 
            out Func<int, EditModel> getEditModelFunc)
        {
            //注入 service
            thiService = ()=> BizSupplierService;

            //初始化 indexmodel
            getIndexModelFunc = () => 
                {
                    return new IndexModel { Areas = BizAreaService.FindAll().ToList() };
                };

            //初始化 addmodel
            getAddModelFunc = () =>
            {
                var query = BizAreaService.FindAll();
                return new AddModel
                {
                    AreaSelectList = new SelectList(query, "Id", "Name"),
                    //QuotePriceTemplates = BizQuotePriceTemplateService.FindAll().ToList()
                };
            };

            //初始化 editmodel
            getEditModelFunc = id =>
            {
                var query = BizAreaService.FindAll();
                var model = BizSupplierService.FindModelById(id);
                if (model == null)
                {
                    throw new Exception("你要修改的数据可能已被移除");
                }
                var editModel = new EditModel
                {
                    AreaSelectList = new SelectList(query, "Id", "Name", model.AreaId),
                    //QuotePriceTemplates = BizQuotePriceTemplateService.FindAll().Where(o=>o.Id==model.AreaId).ToList(),
                    //PriceTemplatesAry = model.QuotePriceTemplates.Select(o=>o.Id).ToArray(),
                    Id = id,
                    AreaId = model.AreaId,
                    Contacter = model.Contacter,
                    EMail = model.EMail,
                    Name = model.Name,
                    Status = 0,
                    Tel = model.Tel,
                    Auditing = model.Auditing
                };
                return editModel;
            };
        }
        #endregion

        #region Methods 方法
        protected override bool EditHandler(EditModel dto)
        {
            var model = BizSupplierService.FindModelById(dto.Id);
            if (model == null)
            {
                throw new Exception("你要修改的数据可能已被移除");
            }
            
            model.Contacter = dto.Contacter;
            model.EMail = dto.EMail;
            model.Name = dto.Name;
            model.Tel = dto.Tel;
            model.Auditing = dto.Auditing;
            if (dto.AreaId != model.AreaId)
            {
                model.QuotePriceTemplates.Clear();
                model.QuotePriceTemplates = new List<BizQuotePriceTemplate>();
            }
            model.AreaId = dto.AreaId;
            BizSupplierService.Update(model);
            return true;
        }

        protected override long AddHandler(AddModel addModel)
        {
            var model = new BizSupplier()
            {
                AreaId = addModel.AreaId,
                Contacter = addModel.Contacter,
                EMail = addModel.EMail,
                Name = addModel.Name,
                Tel = addModel.Tel,
                Auditing = addModel.Auditing
            };
            return BizSupplierService.Add(model);
        }

        protected override IQueryable<BizSupplier> QueryHandler(QueryModel queryModel, IQueryable<BizSupplier> queryable)
        {
            if(!queryModel.Name.IsNullOrEmpty())
                queryable = queryable.Where(o => o.Name.Contains(queryModel.Name));
            if (queryModel.AreaId > 0)
            {
                queryable = queryable.Where(o => o.AreaId==queryModel.AreaId);
            }
            return queryable;
        }
        #endregion

        public ActionResult ChooseMaterial(int id)
        {
            var query = BizAreaService.FindAll();
            var model = BizSupplierService.FindModelById(id);
            if (model == null)
            {
                throw new Exception("你要修改的数据可能已被移除");
            }
            var dto = new ChooseModel
            {
               // AreaSelectList = new SelectList(query, "Id", "Name", model.AreaId),
                QuotePriceTemplates = BizQuotePriceTemplateService.FindAll().Where(o => o.AreaId == model.AreaId).ToList(),
                PriceTemplatesAry = model.QuotePriceTemplates.Select(o => o.Id).ToArray(),
                Id = id,
                AreaId = model.AreaId,
                AreaName = model.BizArea.Name,
                Contacter = model.Contacter,
                EMail = model.EMail,
                Name = model.Name,
                Status = 0,
                Tel = model.Tel,
                Auditing = model.Auditing
            };
            return View(dto);
        }

        public JsonResult SaveChooseMaterial(ChooseModel dto)
        {
            var model = BizSupplierService.FindModelById(dto.Id);
            if (model == null)
            {
                throw new Exception("你要修改的数据可能已被移除");
            }
            model.QuotePriceTemplates.Clear();
            model.QuotePriceTemplates = dto.PriceTemplatesAry == null
                ? new List<BizQuotePriceTemplate>()
                : BizQuotePriceTemplateService.FindAll()
                    .Where(o => o.AreaId == model.AreaId && dto.PriceTemplatesAry.Contains(o.Id)).ToList();
            BizSupplierService.Update(model);
            return new JsonResult { Data = new { success = true } };
        }
    }
}
